package com.bonc.rms.controller.tms;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import scala.annotation.varargs;

import com.alibaba.fastjson.JSON;
import com.bonc.rms.service.tms.IOrganizationService;

@Controller
@RequestMapping(value = "/organizationController")
public class OrganizationController {
	
	@Autowired
	IOrganizationService organizationService;
	
	@RequestMapping(value = "/showOrganization")
	@ResponseBody
	public String showOrganization(
			@RequestParam(value = "departmentId") Integer departmentId, 
			@RequestParam(value = "date") String date
			) {
		return JSON.toJSONString(genResultData(organizationService.getOrganizationByParentId(departmentId, date)));
	}

	@RequestMapping(value = "/showTree")
	@ResponseBody
	public String showTree() {
		
		Map<String, Object> dataMap = new HashMap<String, Object>();
		
		
		Map<String, Object> childMap = new HashMap<String, Object>();
		
		List<Map<String, Object>> childs = new ArrayList<Map<String, Object>>();	
		
		Map<String, Object> childMap11 = new HashMap<String, Object>();
		childMap11.put("name", "杭州");
		Map<String, Object> childMap12 = new HashMap<String, Object>();
		childMap12.put("name", "宁波");
		Map<String, Object> childMap13 = new HashMap<String, Object>();
		childMap13.put("name", "绍兴");
		
		childs.add(childMap11);
		childs.add(childMap12);
		childs.add(childMap13);
		
		childMap.put("name", "浙江");
		childMap.put("children", childs);
		
		dataMap.put("name", "中国");
		dataMap.put("children", childMap);
		
		return JSON.toJSONString(dataMap);
	}
	
	
	/**
	 * 将查出的结果转为前台所需格式
	 * 
	 * @param data
	 * @return
	 */
	private List<Map<String, Object>> genResultData(List<Map<String, Object>> data) {
		
		if(data == null || data.size() == 0) {
			return null;
		}
		
		List<Map<String, Object>> retList = new ArrayList<Map<String, Object>>();
		Map<String, Object> dataMap = null;
		
		if(data.size() == 1) {
			dataMap = new HashMap<String, Object>();
			Map<String, Object> org = data.get(0);
			dataMap.put("name", org.get("name"));
			dataMap.put("id", org.get("id"));
			dataMap.put("value", 4);
			dataMap.put("index", 0);
			dataMap.put("children", new ArrayList<>());
		} else {
			dataMap = new HashMap<String, Object>();
			Map<String, Object> orgRoot = data.get(0);
			dataMap.put("name", orgRoot.get("name"));
			dataMap.put("id", orgRoot.get("id"));
			dataMap.put("value", 6);
			data.remove(0);
			List<Map<String, Object>> childs = new ArrayList<Map<String, Object>>();
			Map<String, Object> nodeMap = null;
			Map<String, Object> node = null;
			for(int i = 0; i < data.size(); i ++) {
				node = data.get(i);
				nodeMap = new HashMap<String, Object>();
				nodeMap.put("name", node.get("name"));
				nodeMap.put("id", node.get("id"));
				nodeMap.put("value", (Integer) node.get("leaf") == 1 ? 4 : 6);
				nodeMap.put("index", i);
				nodeMap.put("children", new ArrayList<>());
				childs.add(nodeMap);
			}
			dataMap.put("index", 0);
			dataMap.put("children", childs);
		}
		retList.add(dataMap);
		
		return retList;
	}
	
}
